# Copyright 2021 Nikita Kniazev
#  Copyright 2020 Rene Rivera
#  Copyright (c) 2003      Michael Stevens
#  Copyright (c) 2010-2011 Bryce Lelbach (blelbach@cct.lsu.edu, maintainer)
#
#  Use, modification and distribution is subject to the Boost Software
#  License Version 1.0. (See accompanying file LICENSE.txt or
#  https://www.bfgroup.xyz/b2/LICENSE.txt)

import common ;
import toolset ;
import feature ;
import toolset : flags ;

import clang ;
import gcc ;
import common ;
import errors ;
import generators ;
import type ;
import numbers ;
import os ;
import property ;
import set ;

feature.extend-subfeature toolset clang : platform : linux ;

toolset.inherit-generators clang-linux
    <toolset>clang <toolset-clang:platform>linux : gcc
  : gcc.mingw.link gcc.mingw.link.dll gcc.cygwin.link gcc.cygwin.link.dll ;
generators.override clang-linux.prebuilt : builtin.lib-generator ;
generators.override clang-linux.prebuilt : builtin.prebuilt ;
generators.override clang-linux.searched-lib-generator : searched-lib-generator ;

# Override default do-nothing generators.
generators.override clang-linux.compile.c.pch   : pch.default-c-pch-generator   ;
generators.override clang-linux.compile.c++.pch : pch.default-cpp-pch-generator ;

toolset.inherit-rules clang-linux : gcc ;
toolset.inherit-flags clang-linux : gcc
  : <inlining>full
    <threading>multi/<target-os>windows
    <lto>on/<lto-mode>full
    <lto>on/<lto-mode>fat
  ;

if [ MATCH (--debug-configuration) : [ modules.peek : ARGV ] ] {
  .debug-configuration = true ;
}

rule init ( version ? :  command * : options * ) {
  command = [ common.find-compiler clang-linux : clang++ : $(version) : $(command) ] ;
  local command-string = [ common.make-command-string $(command) ] ;
  if ! $(version) { # ?= operator does not short-circuit
  version ?= [ get-short-version $(command-string) ] ;
  }

  local condition = [ common.check-init-parameters clang-linux
    : version $(version) ] ;

  common.handle-options clang-linux : $(condition) : $(command) : $(options) ;
  clang.init-cxxstd-flags clang-linux : $(condition) : $(version) ;

  # Support for gcc root as the backend, this is mainly useful for clang/gcc on Windows
  # since on Linux gcc will be the default compiler already located on the PATH.
  # On Windows it is possible to have multiple versions of mingw(-64)/gcc installed
  # in different directories. The <root>option can be given so that the gcc backend
  # can be found at runtime, while the $(command) can be a script that sets the
  # PATH for both the clang directory and the backende gcc directory
  # before calling clang++ when compiling/linking.

  local root = [ feature.get-values <root> : $(options) ] ;

  if $(root)
    {
        # On multilib 64-bit boxes, there are both 32-bit and 64-bit libraries
        # and all must be added to LD_LIBRARY_PATH. The linker will pick the
        # right onces. Note that we do not provide a clean way to build a 32-bit
        # binary using a 64-bit compiler, but user can always pass -m32
        # manually.
        local lib_path = $(root)/bin $(root)/lib $(root)/lib32 $(root)/lib64 ;
        if $(.debug-configuration)
        {
            ECHO "notice:" using gcc libraries with clang"::" $(condition) "::" $(lib_path) ;
        }
        toolset.flags clang-linux.link RUN_PATH $(condition) : $(lib_path) ;
    }

  # - Archive builder.
  local archiver = [ feature.get-values <archiver> : $(options) ] ;
  if ( ! $(archiver) ) && $(root)
    {
    archiver = $(root)/bin/ar ;
    }
  toolset.flags clang-linux.archive .AR $(condition) : $(archiver[1]) ;
}

rule get-full-version ( command-string )
{
    return [ common.match-command-output version : "([0-9]+.[0-9]+.[0-9]+)"
           : "$(command-string) --version" ] ;
}

rule get-short-version ( command-string : single-digit-since ? )
{
    local version = [ get-full-version $(command-string) ] ;
    version = [ SPLIT_BY_CHARACTERS $(version) : . ] ;

    import version ;
    if [ version.version-less $(version) : $(single-digit-since:E=4) ]
    {
        return $(version[1-2]:J=.) ;
    }

    return $(version[1]) ;
}

###############################################################################
# Flags

local all-os = [ feature.values <target-os> ] ;

# note: clang silently ignores some of these inlining options
# For clang, 'on' and 'full' are identical.
toolset.flags clang-linux.compile OPTIONS <inlining>full : -Wno-inline ;

toolset.flags clang-linux.compile OPTIONS <threading>multi/<target-os>windows : -pthread ;
toolset.flags clang-linux.link OPTIONS <threading>multi/<target-os>windows : -pthread ;

# LTO
toolset.flags clang-linux.compile OPTIONS <lto>on/<lto-mode>thin : -flto=thin ;
toolset.flags clang-linux.link OPTIONS <lto>on/<lto-mode>thin : -flto=thin ;

toolset.flags clang-linux.compile OPTIONS <lto>on/<lto-mode>full : -flto=full ;
toolset.flags clang-linux.link OPTIONS <lto>on/<lto-mode>full : -flto=full ;

# stdlib selection
toolset.flags clang-linux.compile OPTIONS <stdlib>gnu <stdlib>gnu11 : -stdlib=libstdc++ ;
toolset.flags clang-linux.link OPTIONS <stdlib>gnu <stdlib>gnu11 : -stdlib=libstdc++ ;

toolset.flags clang-linux.compile OPTIONS <stdlib>libc++ : -stdlib=libc++ ;
toolset.flags clang-linux.link OPTIONS <stdlib>libc++ : -stdlib=libc++ ;

# Enable response file control
toolset.flags clang-linux RESPONSE_FILE_SUB <response-file>auto : a ;
toolset.flags clang-linux RESPONSE_FILE_SUB <response-file>file : f ;
toolset.flags clang-linux RESPONSE_FILE_SUB <response-file>contents : c ;

# Used in actions for multi argument options
_ = " " ;
###############################################################################
# C and C++ compilation

rule compile.c++ ( targets * : sources * : properties * ) {
  DEPENDS $(<) : [ on $(<) return $(PCH_FILE) ] ;
}

actions compile.c++ bind PCH_FILE
{
  "$(CONFIG_COMMAND)" -c -x c++ $(OPTIONS) $(USER_OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -Xclang$(_)-include-pch$(_)-Xclang$(_)"$(PCH_FILE)" -include"$(FORCE_INCLUDES)" -o "$(<)" "$(>)"
}

rule compile.c ( targets * : sources * : properties * )
{
  DEPENDS $(<) : [ on $(<) return $(PCH_FILE) ] ;
}

actions compile.c bind PCH_FILE
{
  "$(CONFIG_COMMAND)" -c -x c $(OPTIONS) $(USER_OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -Xclang$(_)-include-pch$(_)-Xclang$(_)"$(PCH_FILE)" -include"$(FORCE_INCLUDES)" -c -o "$(<)" "$(>)"
}

###############################################################################
# Linking

local soname-os = [ set.difference $(all-os) : windows ] ;
toolset.flags clang-linux.link SONAME_OPT <target-os>$(soname-os) : "-Wl,-soname -Wl," ;

actions link bind LIBRARIES {
    "$(CONFIG_COMMAND)" -L"$(LINKPATH)" -o "$(<)" @($(<[1]:T).rsp:O=FC:<=@":>=":E=-Wl,-R$(_)-Wl,"$(RPATH)" -Wl,-rpath-link$(_)-Wl,"$(RPATH_LINK)" $(START-GROUP) "$(>:T)" "$(LIBRARIES:T)" $(FINDLIBS-ST-PFX:T) -l$(FINDLIBS-ST:T) $(FINDLIBS-SA-PFX:T) -l$(FINDLIBS-SA:T) $(END-GROUP)) $(OPTIONS) $(USER_OPTIONS)
}

actions link.dll bind LIBRARIES {
    "$(CONFIG_COMMAND)" -L"$(LINKPATH)" -o "$(<)" @($(<[1]:T).rsp:O=FC:<=@":>=":E=-Wl,-R$(_)-Wl,"$(RPATH)" $(SONAME_OPT)$(<[1]:D=) -shared $(START-GROUP) "$(>:T)" "$(LIBRARIES:T)" $(FINDLIBS-ST-PFX:T) -l$(FINDLIBS-ST:T) $(FINDLIBS-SA-PFX:T) -l$(FINDLIBS-SA:T) $(END-GROUP)) $(OPTIONS) $(USER_OPTIONS)
}
